package com.ailk.tcm.hffw.android.common;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import com.ailk.tcm.R;

public class MyNotificationManager {
	private static final int NOTIFY_ID = 11987;
	private static int INTENT_REQUEST_CODE = 0;
	private Context context;
	private NotificationManager notificationManager;
	public MyNotificationManager(Context context){
		this.context = context;
		notificationManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
	}
	
	public void notify2Activity(String from, String title, 
			String content, Intent intent){
		PendingIntent pendingIntent = null;
		if(intent != null){
			pendingIntent = PendingIntent.getActivity(context, INTENT_REQUEST_CODE++,
					intent, PendingIntent.FLAG_ONE_SHOT);
		}
		notification(from, title, content, pendingIntent);
	}
	
	public void notify2Service(String from, String title, 
			String content, Intent intent){
		PendingIntent pendingIntent = null;
		if(intent != null){
			pendingIntent = PendingIntent.getService(context, INTENT_REQUEST_CODE++,
					intent, PendingIntent.FLAG_ONE_SHOT);
		}
		notification(from, title, content, pendingIntent);
	}
	
	public void notify2Broadcast(String from, String title, 
			String content, Intent intent){
		PendingIntent pendingIntent = null;
		if(intent != null){
			pendingIntent = PendingIntent.getBroadcast(context, INTENT_REQUEST_CODE++,
					intent, PendingIntent.FLAG_ONE_SHOT);
		}
		notification(from, title, content, pendingIntent);
	}
	
	public void notification(String from, String title, 
			String content, PendingIntent pendingIntent){
		Notification n = new Notification(R.drawable.ic_launcher, 
				context.getString(R.string.app_name) + context.getString(R.string.get_notification),
				System.currentTimeMillis());
		n.setLatestEventInfo(context, title, content, pendingIntent);
		n.flags = Notification.FLAG_AUTO_CANCEL; // 被点击后自动清除
		n.defaults |= Notification.DEFAULT_SOUND;
		notificationManager.notify(from, NOTIFY_ID, n);
	}
}
